<?php
/**
 * 管理中心· 广告管理模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2020 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com；
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Hoteladmin\Controller;
use Hoteladmin\Model\AdModel;
use Think\AjaxPage;
class AdController extends BaseController {
	/**
	 +--------------------------------------------------------------------------
	 * 实现获取广告位置的列表
	 * @author : XC跃
	 * @date：2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function posiList(){
		$this->display();
	}
	public function ajaxPosiList(){
	    //实例化相关表
	    $apident = M('ad_position');
	    // 搜索条件
	    $condition = array();
	    I('pname') ? $condition['po_name'] = array('LIKE',"%".I('pname')."%") : false;
	    //dump($condition);
	    $sort_order = I('order_by').' '.I('sort');
	    //echo $sort_order;
	    $count = $apident->where($condition)->count();
	    $Page  = new AjaxPage($count,50);
	    $show = $Page->show();
	    $list = $apident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
	    //echo $usident->getLastSql();
	    $this->assign('list',$list);
	    $this->assign('page',$show);// 赋值分页输出
	    $this->assign('rows',$Page->totalRows);
	    $this->display();
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现广告位置的添加(修改)功能
	 * @author : XC跃
	 * @date：2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function posiAddEdit(){
	    $pid = I('pid/d');
	    //实例化相关表
	    $pident = M('ad_position');
	    //实例化相关Model
	    $aidnetr = New AdModel();
	    if(IS_POST){
	        $re = $aidnetr->posiAddEdit($pid);
	        if($re){
	            $res = array('status'=>1,'msg'=>'保存成功');
	        }else{
	            $res = array('status'=>-1,'msg'=>'未做更改');
	        }exit(json_encode($res));
	    }else{
	        if(!empty($pid)){
	            $res = $pident->where("`po_id`='".$pid."'")->find();
	            $this->assign('res',$res);
	        }
	        $this->display();
	    }
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现获取广告图片的列表
	 * @author : XC跃
	 * @date：2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function adList(){
	    $this->assign('pid',I("pid/d"));
	    $this->display();
	}
	public function ajaxAdList(){
	    //实例化相关表
	    $adident = M('ad');
	    $apident = M('ad_position');
	    // 搜索条件
	    $condition = array();
	    I('pname') ? $condition['ad_name'] = array('LIKE',"%".I('pname')."%") : false;
	    I('pid') ? $condition['ad_pid'] = I('pid') : false;
	    //dump($condition);
	    $sort_order = I('order_by').' '.I('sort');
	    //echo $sort_order;
	    $count = $adident->where($condition)->count();
	    $Page  = new AjaxPage($count,50);
	    $show = $Page->show();
	    $list = $adident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
	    foreach($list as $key=>$val){
	        $list[$key]['pname'] = $apident->where("`po_id`='".$val['ad_pid']."'")->getField("po_name");
	    }
	    $this->assign('list',$list);
	    $this->assign('page',$show);// 赋值分页输出
	    $this->assign('rows',$Page->totalRows);
	    $this->display();
	}
    /**
     +--------------------------------------------------------------------------
     * 实现广告图片的添加(修改)功能
     * @author : XC跃
     * @date：2018-09-28
     +--------------------------------------------------------------------------
     */
    public function adAddEdit(){
        $aid = I('aid/d');
        //实例化相关表
        $aident = M('ad');
        $pident = M('ad_position');
        //实例化相关Model
        $aidnetr = New AdModel();
        if(IS_POST){
            $re = $aidnetr->adAddEdit($aid);
            if($re){
                $res = array('status'=>1,'msg'=>'保存成功');
            }else{
	            $res = array('status'=>-1,'msg'=>'未做更改');
	        }exit(json_encode($res));
        }else{            
            if(!empty($aid)){
                $res = $aident->where("`ad_id`='".$aid."'")->find();
                $this->assign('res',$res);
            }
            $pre = $pident->where("`po_open`=1")->select();
            $this->assign('type',$pre);
            $this->display();
        }
    }
    /**
     +--------------------------------------------------------------------------
     * 实现广告图片的删除功能
     * @author : XC跃
     * @date：2018-09-28
     +--------------------------------------------------------------------------
     */
    public function delAd(){
        //接收相关值
        $id = I('post.id/d');
        //实例化相关表
        $aident = M('ad');
        delPath("ad","ad_id=$id","ad_url");   //清除图片
        $re = $aident->where("`ad_id`='".$id."'")->delete();
        if($re>0){
            $res = array('status'=>1,'msg'=>'删除成功');
            exit(json_encode($res));
        }
    }
}